<template>
	<div class="login_container">
		<el-row>
			<el-col :span="12" :xs="0">
				 <h1>Hello! 欢迎来到通用后台管理系统</h1>
			</el-col>
			<el-col :span="12" :xs="24">
				<el-card class="box-card">
					<el-tabs v-model="activeName" class="demo-tabs" :stretch='true'>
						<el-tab-pane label="登录" name="first">
							<el-form class="login-form">
								<el-form-item label="账号">
									<el-input v-model="loginData.account" placeholder="请输入账号" />
								</el-form-item>
								<el-form-item label="密码">
									<el-input v-model="loginData.password" placeholder="请输入密码" show-password />
								</el-form-item>
								<!-- 底部外壳 -->
								<div class="footer-wrapped">
									<div class="forget-password">
										<span class="forget-password-button" @click="openForget">忘记密码</span>
									</div>
									<div class="footer-button">
										<el-button type="primary" @click="Login">登录</el-button>
									</div>
									<div class="footer-go-register">
										还没有账号？<span class="go-register">马上注册</span>
									</div>
								</div>
							</el-form>
						</el-tab-pane>

						<el-tab-pane label="注册" name="second">
							<el-form class="login-form">
								<el-form-item label="账号">
									<el-input v-model="registerData.account" placeholder="账号长度6-12位" />
								</el-form-item>
								<el-form-item label="密码">
									<el-input v-model="registerData.password" placeholder="密码需长度6-12位含字母数字" />
								</el-form-item>
								<el-form-item label="确认密码">
									<el-input v-model="registerData.nextPassword" placeholder="请再次输入密码" />
								</el-form-item>
								<div class="footer-button">
									<el-button type="primary" @click="Register">注册</el-button>
								</div>
							</el-form>
						</el-tab-pane>
					</el-tabs>
				</el-card>
			</el-col>
		</el-row>
	</div>
	<forget ref="forgetP"></forget>
</template>

<script lang="ts" setup>
	import { ref, reactive } from 'vue'
	import { ElMessage } from 'element-plus'
	import { useRouter } from 'vue-router'//导入路由跳转
	import forget from './components/forget_password.vue'
	// 调用登录接口,注册接口
	import {
		login, register
	} from '@/api/login'
	import { useUserInfor } from '@/store/userinfor'
	const activeName = ref('first')
	const router = useRouter() // 路由跳转的实例
	const store = useUserInfor()
	// 表单接口
	interface formData {
		account : number | null;
		password : string;
		nextPassword ?: string;
	}
	// 登录表单数据
	const loginData : formData = reactive({
		account: null,
		password: '',
	})
	// 注册表单数据
	const registerData : formData = reactive({
		account: null,
		password: '',
		nextPassword: '',
	})

	// 登录
	const Login = async () => {
		// loginData:登陆的表单数据
		const res = await login(loginData)
		// console.log(res.data.results)
		// 登陆会拿到一个token,采用解构赋值的形式来获取
		const { id } = res.data.results
		const { token } = res.data
		if (res.data.message == "登录成功") {
			ElMessage({
				message: '登录成功',
				type: 'success',
			})
			sessionStorage.setItem('id', id)
			localStorage.setItem('token', token)//jiangtoken存储到localStorage里面
			store.userInfor(id)
			// 跳转
			router.push('/home')
		} else {
			ElMessage.error('登录失败')
		}
	}

	// 注册,因为返回的是一个promise,所以需要使用async await
	// 注册
	const Register = async () => {
		if (registerData.password == registerData.nextPassword) {
			const res = await register(registerData)
			console.log(res.data)
			if (res.data.message == "注册账号成功") {
				ElMessage({
					message: '注册成功',
					type: 'success',
				})
				activeName.value = 'first'
			} else {
				ElMessage.error('注册失败，请检查数据是否正确')
			}
		} else {
			ElMessage.error('注册失败')
		}
	}

	// 忘记密码弹窗
	const forgetP = ref()
	// 打开忘记密码弹窗
	const openForget = () => {
		forgetP.value.open()
	}
</script>

<style lang="scss" scoped>
	 h1 {
	      color: white;
	      font-size: 40px;
		  margin-top: 100px;
		  margin-left: 30px;
	    }
	.login_container {
		height: 100vh;
		background: url('@/assets/background.jpg') no-repeat;
		background-size: cover;

		// 卡片样式
		.box-card {
			width: 350px;
			height: 375px;
			position: relative;
			top: 48%;
			left: 35%;

			.login-form {

				// 登录底部外壳
				.footer-wrapped {
					display: flex;
					flex-direction: column;

					// 忘记密码
					.forget-password {
						display: flex;
						justify-content: flex-end;
						margin: 10px 0;

						// 登录按钮
						.forget-password-button {
							font-size: 14px;
							color: #409eff;
							// 鼠标移入的效果
							cursor: pointer;
						}
					}


					.footer-go-register {
						font-size: 12px;
						margin: 12px 0;
						display: flex;
						justify-content: center;

						.go-register {
							font-size: 13px;
							color: #409eff;
							cursor: pointer;
						}
					}
				}

				// 底部登录按钮
				.footer-button {
					width: 100%;
					display: flex;
					justify-content: center;
				}
			}
		}

		// 表单边距
		.el-form {
			margin-top: 30px;
		}

		// tabs标签
		:deep(.el-tabs__item) {
			color: #333;
			font-size: 18px;
		}

		// 输入框高度
		:deep(.el-input__inner) {
			height: 40px;
		}

		// 输入框标签字体高度
		:deep(.el-form-item__label) {
			height: 40px;
			line-height: 40px;
			font-size:18px;
		}

		// 登录按钮
		:deep(.el-button) {
			width: 300px;
			height: 45px;
			font-size: 16px;
		}
	}
</style>